package com.songlanyun.modules.coupon.controller.admin;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.songlanyun.modules.coupon.model.dto.CouponEntityDTO;
import com.songlanyun.modules.shop.entity.ShopEntity;
import com.songlanyun.modules.shop.service.ShopService;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.modules.coupon.entity.CouponEntity;
import com.songlanyun.modules.coupon.service.CouponService;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;



/**
 * 优惠券
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2022-07-05 15:20:02
 */
@RestController
@RequestMapping("admin/coupon")
public class CouponController extends AbstractController {
    @Autowired
    private CouponService couponService;

    @Autowired
    private ShopService shopService;

    /**
     * 列表
     * name 名称
         * trait   -1 全部，0:普通券，1:新人券
     * mode  -1 全部， 优惠券范围 0:平台券，1:店铺券
     * status -1 全部  *  0: 已终止  1：待发布 2：已发布 3: 正常  4：已完成 
     */
    @RequestMapping("/page")
    //@RequiresPermissions("coupon:coupon:list")
    public R list(@RequestParam Map<String, Object> params){
        Long shopId = getShopId();
        params.put("shop_id",shopId);
        PageUtils page = couponService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("coupon:coupon:info")
    public R info(@PathVariable("id") Long id){
		CouponEntity coupon = couponService.loadById(id,true);
        coupon.loadAdaptShops();
        coupon.loadAdaptGoods();
        return R.ok().put("data", coupon);
    }

    /**
     * 保存
     */
    @PostMapping("/add")
    //@RequiresPermissions("coupon:coupon:save")
    public R save(@RequestBody CouponEntityDTO dto){
        dto.setShopId(getShopId());
		couponService.add(dto);

        return R.ok();
    }

    /**
     * 发布优惠券
     */
    @PostMapping("/released/{id}")
    //@RequiresPermissions("coupon:coupon:save")
    public R released(@PathVariable("id") Long id ){
        couponService.released(id,getShopId());

        return R.ok();
    }

    /**
     * 取消发布优惠券
     */
    @PostMapping("/cancelReleased/{id}")
    //@RequiresPermissions("coupon:coupon:save")
    public R cancelReleased(@PathVariable("id") Long id ){
        couponService.cancelReleased(id,getShopId());

        return R.ok();
    }

    /**
     * 停止优惠券活动
     */
    @PostMapping("/stop/{id}")
    //@RequiresPermissions("coupon:coupon:save")
    public R shop(@PathVariable("id") Long id ){
        couponService.stop(id,getShopId());

        return R.ok();
    }

    /**
     * 恢复优惠券活动
     */
    @PostMapping("/cancelStop/{id}")
    //@RequiresPermissions("coupon:coupon:save")
    public R cancelStop(@PathVariable("id") Long id ){
        couponService.cancelStop(id,getShopId());

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    //@RequiresPermissions("coupon:coupon:update")
    public R update(@RequestBody CouponEntityDTO dto){
        dto.setShopId(getShopId());
		couponService.edit(dto);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    //@RequiresPermissions("coupon:coupon:delete")
    public R delete(@RequestBody Long[] ids){
        couponService.batchRemoveByIds(Arrays.asList(ids));
        return R.ok();
    }

}
